package com.yanqu.road.dao.impl.tradewar;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.dao.impl.logic.ResultSetParseEntityManger;
import com.yanqu.road.entity.tradewar.TradeWarTrackRecord;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossTradeWarTrackRecordDaoImpl extends TempDataObjectDao<TradeWarTrackRecord> {

	public Map<String, List<TradeWarTrackRecord>> getTradeWarTrackRecordMap(int activityId) {
		ResultSet rs = null;
		CallableStatement call = null;
		Map<String, List<TradeWarTrackRecord>> dataMap = new ConcurrentHashMap<>();
		String sql = "select * from t_c_trade_war_track_record where activityId = ?";
		try {
			getDb().setConnName(DbNameStrategy.getCross2Db());
			call = getDb().getConnection().prepareCall(sql);
			call.setInt(1,activityId);
			rs = call.executeQuery();
			while (rs.next()) {
				TradeWarTrackRecord data = parse(rs);
				if(!dataMap.containsKey(data.getWantedUid())){
					dataMap.put(data.getWantedUid(), new ArrayList<>());
				}
				dataMap.get(data.getWantedUid()).add(data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public Map<String, List<TradeWarTrackRecord>> getTradeWarTrackRecordMap(List<Integer> activityIdList) {
		ResultSet rs = null;
		CallableStatement call = null;
		Map<String, List<TradeWarTrackRecord>> dataMap = new ConcurrentHashMap<>();
		if(null == activityIdList || activityIdList.size() <= 0){
			return dataMap;
		}
		String sql = "select * from t_c_trade_war_track_record where activityId in("+ StringUtils.listToString(activityIdList, ",") + ")";
		try {
			getDb().setConnName(DbNameStrategy.getCross2Db());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				TradeWarTrackRecord data = parse(rs);
				if(!dataMap.containsKey(data.getWantedUid())){
					dataMap.put(data.getWantedUid(), new ArrayList<>());
				}
				dataMap.get(data.getWantedUid()).add(data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	@Override
	protected boolean addData(TradeWarTrackRecord data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_c_trade_war_track_record(recordUid,wantedUid,userId,userBaseInfo,killPatronsCount,loseScore,recordTime," +
				"myAddScore,tradeWarMoney,activityId) VALUES(?,?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getCross2Db());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setString(i++, data.getRecordUid());
			call.setString(i++, data.getWantedUid());
			call.setLong(i++, data.getUserId());
			call.setString(i++, data.getUserBaseInfoJsonStr());
			call.setInt(i++, data.getKillPatronsCount());
			call.setInt(i++, data.getLoseScore());
			call.setLong(i++, data.getRecordTime());
			call.setInt(i++, data.getMyAddScore());
			call.setInt(i++, data.getTradeWarMoney());
			call.setInt(i++, data.getActivityId());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(TradeWarTrackRecord data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_c_trade_war_track_record SET wantedUid=?,userId=?,userBaseInfo=?,killPatronsCount=?,loseScore=?,recordTime=?," +
				"myAddScore=?,tradeWarMoney=?,activityId=? WHERE recordUid=?";
		try {
			getDb().setConnName(DbNameStrategy.getCross2Db());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setString(i++, data.getWantedUid());
			call.setLong(i++, data.getUserId());
			call.setString(i++, data.getUserBaseInfoJsonStr());
			call.setInt(i++, data.getKillPatronsCount());
			call.setInt(i++, data.getLoseScore());
			call.setLong(i++, data.getRecordTime());
			call.setInt(i++, data.getMyAddScore());
			call.setInt(i++, data.getTradeWarMoney());
			call.setInt(i++, data.getActivityId());
			call.setString(i++, data.getRecordUid());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected TradeWarTrackRecord parseData(ResultSet rs) throws SQLException {
		TradeWarTrackRecord data = ResultSetParseEntityManger.initTradeWarTrackRecord(rs);
		data.setActivityId(rs.getInt("activityId"));
		return data;
	}
}
